// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: DServer support simple entity
// ------------------------------------------------------------------

#ifndef XMLPARSER_PORTPARAMETERS_HPP_
#define XMLPARSER_PORTPARAMETERS_HPP_
#include <iostream>
#include <ertps/types/TypesBase.h>

namespace vbsutil {
namespace xmlparser {

using vbs::common::types::DOMAIN_ID_MAX;
using vbs::common::types::DomainId_t;
class PortParameters {
 public:
    PortParameters()
        : portBase(7400U),
          domainIDGain(250U),
          participantIDGain(2U),
          offsetd0(0U),
          offsetd1(10U),
          offsetd2(1U),
          offsetd3(11U) {}

    virtual ~PortParameters() {}

    bool operator==(const PortParameters& b) const {
        return (this->portBase == b.portBase) && (this->domainIDGain == b.domainIDGain) &&
               (this->participantIDGain == b.participantIDGain) && (this->offsetd0 == b.offsetd0) &&
               (this->offsetd1 == b.offsetd1) && (this->offsetd2 == b.offsetd2) && (this->offsetd3 == b.offsetd3);
    }

    /**
     * Get a multicast port based on the domain ID.
     *
     * @param domainId Domain ID.
     * @return Multicast port
     */
    inline uint32_t getMulticastPort(const uint32_t domainId) const {
        const uint32_t port = portBase + (domainIDGain * domainId) + offsetd0;

        if (port > 65535U) {

            std::cout << "Calculated port number is too high. Probably the domainId is over 232 "
                      << "or portBase is too high." << std::endl;
            (void)std::cout.flush();
            exit(EXIT_FAILURE);
        }

        return port;
    }

    /**
     * Get a unicast port based on the domain ID and the participant ID.
     *
     * @param domainId Domain ID.
     * @param RTPSParticipantID Participant ID.
     * @return Unicast port
     */
    inline uint32_t getUnicastPort(const uint32_t domainId, const uint32_t RTPSParticipantID) const {
        uint32_t port = portBase + (domainIDGain * domainId) + offsetd1 + (participantIDGain * RTPSParticipantID);

        if (port > 65535U) {
            std::cout << "Calculated port number is too high. Probably the domainId is over 232, "
                         "there are "
                      << "too much participants created or portBase is too high." << std::endl;
            (void)std::cout.flush();
            exit(EXIT_FAILURE);
        }

        return port;
    }

    /**
     * 通过PDP接收端口解析Domain ID
     * @param port 接收到的端口号
     * @return 成功返回非负Domain ID，失败返回 DOMAIN_ID_UNKNOWN
     */
    inline DomainId_t getDomainId(int16_t port) const {
        if (port < portBase || port > static_cast<uint16_t>(portBase + (DOMAIN_ID_MAX * domainIDGain) + offsetd0)) {
            return vbs::common::types::DOMAIN_ID_UNKNOWN;
        }

        const uint16_t port_diff = port - portBase;

        if (port_diff % domainIDGain != offsetd0) {
            return vbs::common::types::DOMAIN_ID_UNKNOWN;
        }

        const DomainId_t domain_id = port_diff / domainIDGain;

        return (domain_id <= DOMAIN_ID_MAX) ? domain_id : vbs::common::types::DOMAIN_ID_UNKNOWN;
    }

 public:
    uint16_t portBase;

    uint16_t domainIDGain;

    uint16_t participantIDGain;

    uint16_t offsetd0;

    uint16_t offsetd1;

    uint16_t offsetd2;

    uint16_t offsetd3;
};

}  // namespace xmlparser
}  // namespace vbsutil

#endif
